<?php

header('Content-type: application/json');

if (isset($_REQUEST['id']) and is_numeric($_REQUEST['id'])) {
	$folders = $db->fetchAll("SELECT * FROM cms_images_folders WHERE site_id=? AND parent_id=? ORDER BY title", array($site['id'], $_REQUEST['id']));
	
	for($i=0;$i<count($folders);$i++) {
		$folders[$i]['children'] = $db->fetchOne("SELECT COUNT(*) FROM cms_images_folders WHERE parent_id=?", $folders[$i]['id']);
	}
	$json = array();
	foreach ($folders as $folder) {
		$class = '';
		if ($folder['children'] == 0) $class .= 'no-children';
		$json[] = array(
			'attributes' => array(
				'id'        => $folder['id'], 
				'class'     => $class, 
				'rel'       => ($folder['parent_id']==0 ? 'root' : 'folder'), 
				'parent_id' => $folder['parent_id']
			),
			'state' => 'closed',
			'data' => array('title' => $folder['title'], 'icon' => "/admin/backend/resources/images/icons/folder.png")
		);
	}
}

echo json_encode($json);

